# 侧面射击
# 编写一个游戏，将一艘飞船放在屏幕左侧，并允许玩家上下移动飞船。在玩家按空格键时，让飞船发射一颗在屏幕中向右飞行的子弹，并在子弹从屏幕中消失后将其删除。
import pygame
import sys

from ship import Ship
from bullet import Bullet


class RightBullet:

    def __init__(self):
        pygame.init()
        pygame.display.set_caption("侧面射击")
        self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        self.screen_rect = self.screen.get_rect()
        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()

    def run_game(self):
        while True:
            self.screen.fill((255, 255, 255))
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        sys.exit()
                    elif event.key == pygame.K_UP:
                        self.ship.movingUp = True
                    elif event.key == pygame.K_DOWN:
                        self.ship.movingDown = True
                    elif event.key == pygame.K_SPACE:
                        new_bullet = Bullet(self)
                        self.bullets.add(new_bullet)
                elif event.type == pygame.KEYUP:
                    if event.key == pygame.K_UP:
                        self.ship.movingUp = False
                    elif event.key == pygame.K_DOWN:
                        self.ship.movingDown = False
            self.ship.update()
            self.ship.blitme()
            for bullet in self.bullets.copy():
                if bullet.rect.left >= self.screen_rect.right:
                    self.bullets.remove(bullet)
            self.bullets.update()
            for bullet in self.bullets.sprites():
                bullet.draw_bullet()
            pygame.display.flip()


if __name__ == "__main__":
    rightBullet = RightBullet()
    rightBullet.run_game()
